package com.wgts.wgts.mvp.view.activity.setPassWord;

import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerSettingPasswordActivityComponent;
import com.wgts.wgts.dagger.module.SettingPasswordActivityModule;
import com.wgts.wgts.mvp.presenter.ISettingPasswordActivityPresenter;
import com.wgts.wgts.mvp.view.ISettingPasswordActivityView;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/1314:02
 *
 *   设置支付密码页面
 *
 */public class SettingPasswordActivity extends BaseActivity implements ISettingPasswordActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.edit_password_again)
    EditText editPasswordAgain;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.submit_button)
    RelativeLayout submitButton;
    @BindView(R.id.code_text)
    TextView codeText;

    private String password, passwordAgain, code, Type;

    private int SettingPasswordResult = 4;

    @Inject
    ISettingPasswordActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_cursh_password);
        ButterKnife.bind(this);
        initView();
        //DisableCopyAndPaste.getInstance().disableCopyAndPaste(passwordEdit);
    }

    @Override
    public void init() {
        DaggerSettingPasswordActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .settingPasswordActivityModule(new SettingPasswordActivityModule(this))
                .build()
                .inject(this);
    }

    public void initView() {
//        editPassword.setInputType(EditorInfo.TYPE_CLASS_PHONE);
//        editPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
//        editPasswordAgain.setInputType(EditorInfo.TYPE_CLASS_PHONE);
//        editPasswordAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
//        codeEt.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        if (AppInfor.getInstance().isModifyPayPassword()) {
            includeTitle.setText("修改资金密码");
            Type = "UPDATE_USER_PAY_PASSWORD";
        } else {
            includeTitle.setText("设置资金密码");
            Type = "SET_USER_PAY_PASSWORD";
        }
        if (AppInfor.getInstance().getUserPhone() != null && !AppInfor.getInstance().getUserPhone().equals("")){
            StringBuilder builder = new StringBuilder(AppInfor.getInstance().getUserPhone());
            builder.replace(3, 7, "****");
            phone.setText(builder.toString());
        }
    }

    @OnClick({R.id.back_btn_ly, R.id.submit_button, R.id.code_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.submit_button:
                submit();
                break;
            case R.id.code_text:
                codeText.setEnabled(false);
                if (AppInfor.getInstance().isModifyPayPassword()){
                    showDialog();
                    presenter.sendSms(AppInfor.getInstance().getUserPhone(), "UPDATE_PAY_PASSWORD");
                }else {
                    showDialog();
                    presenter.sendSms(AppInfor.getInstance().getUserPhone(), "SET_PAY_PASSWORD");
                }
                presenter.addCountDownTimeListener();
                break;
        }
    }

    private void submit() {
        password = editPassword.getText().toString().trim();
        passwordAgain = editPasswordAgain.getText().toString().trim();
        code = codeEt.getText().toString().trim();
        if (password == null || password.equals("")) {
            MoHuToast.showToast("请输入密码");
            return;
        }else {
            if (password.length() < 6){
                MoHuToast.showToast("请输入6位数字密码");
                return;
            }
        }
        if (passwordAgain == null || passwordAgain.equals("")) {
            MoHuToast.showToast("请输入密码");
            return;
        }else {
            if (passwordAgain.length() < 6){
                MoHuToast.showToast("请输入6位数字密码");
                return;
            }
        }
        if (code == null || code.equals("")) {
            MoHuToast.showToast("请输入验证码");
            return;
        }
        if (!password.equals(passwordAgain)) {
            MoHuToast.showToast("两次输入密码不同");
            return;
        }
        showDialog();
        presenter.settingCrushPassword(password, passwordAgain, code, Type);
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void settingCrushPassword() {
        dialogDismiss();
        MoHuToast.showToast("操作成功");
        AppInfor.getInstance().setModifyPayPassword(true);
        setResult(SettingPasswordResult);
        finish();
    }

    @Override
    public void sendSmsResult() {
        dialogDismiss();
        MoHuToast.showToast("发送成功");
    }

    @Override
    public void countDownTimeOver() {
        codeText.setEnabled(true);
        codeText.setText("重新获取");
        presenter.removeCountDownTimeListener();
    }

    @Override
    public void countDowmTime(final Integer time) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                codeText.setText(String.valueOf(time) + "s");
            }
        });
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        presenter.removeCountDownTimeListener();
    }

}
